/* 
 * File:   QImageLoader.cpp
 * Author: matt
 * 
 * Created on 16 May 2010, 20:29
 */

#include <qt4/QtCore/qtimer.h>

#include "QImageLoader.h"

QImageLoader::QImageLoader() {

    filedialog.setVisible(false);
    filedialog.setFileMode(QFileDialog::ExistingFiles);
    layout = new QGridLayout(this);
    load_button = new QPushButton("Load Images");
    next_button = new QPushButton("Next");
    prev_button = new QPushButton("Prev");
    loop_button = new QPushButton("Loop");
    play_button = new QPushButton("Play");
    timer = new QTimer();
    layout->addWidget(load_button, 0, 0, 1, 4);
    layout->addWidget(prev_button, 1, 0);
    layout->addWidget(next_button, 1, 1);
    layout->addWidget(play_button, 1, 2);
    layout->addWidget(loop_button, 1, 3);
    index = 0;
    loop = false;
    play = false;

    connect(load_button, SIGNAL(clicked(bool)), this, SLOT(showDialog(bool)));
    connect(next_button, SIGNAL(clicked(bool)), this, SLOT(nextImage(bool)));
    connect(prev_button, SIGNAL(clicked(bool)), this, SLOT(prevImage(bool)));
    connect(loop_button, SIGNAL(clicked(bool)), this, SLOT(loopImages(bool)));
    connect(play_button, SIGNAL(clicked(bool)), this, SLOT(playImages(bool)));
    connect(&filedialog, SIGNAL(filesSelected(QStringList)), this, SLOT(openFiles(QStringList)));
    connect(timer, SIGNAL(timeout()), this, SLOT(nextImage()));
}

QImageLoader::~QImageLoader() {
}

void QImageLoader::getImage() {

    if (images.size() == 0) {
        QImage blank;
        blank = QImage(640, 480, QImage::Format_RGB32);
        blank.fill(qRgb(0, 255, 0));
        emit(newImage(blank));
    }
    emit(newImage(images.at(index)));
}

void QImageLoader::openFiles(QStringList selected) {
    images.clear();
    for (int loop = 0; loop < selected.size(); loop++) {
        cout << "Loading image: " << selected.at(loop).toStdString().c_str() << endl;
        QImage current;
        current.load(selected.at(loop));
        images.push_back(current);
    }
    if (images.size() == 0) {
        QImage blank;
        blank = QImage(640, 480, QImage::Format_RGB32);
        blank.fill(qRgb(0, 255, 0));
    }
}

void QImageLoader::showDialog(bool pressed) {
    filedialog.setVisible(true);
}

void QImageLoader::nextImage() {
    if (images.size() > 0) {
        index++;
        if (index >= images.size()) {
            index = 0;
            if (loop == false)timer->stop();
        }
        cerr << "Emiting image: " << index << endl;
        emit(newImage(images.at(index)));
    }
}

void QImageLoader::nextImage(bool pressed) {
    if (images.size() > 0) {
        index++;
        if (index >= images.size()) {
            index = 0;
            if (loop == false)timer->stop();
        }
        cerr << "Emiting image: " << index << endl;
        emit(newImage(images.at(index)));
    }
}

void QImageLoader::prevImage(bool pressed) {
    if (images.size() > 0) {
        index--;
        if (index <= -1)index = images.size() - 1;
        cerr << "Emiting image: " << index << endl;
        emit(newImage(images.at(index)));
    }
}

void QImageLoader::playImages(bool pressed) {
    if (images.size() > 0) {
        cerr << "Playing images" << endl;
        loop = false;
        play = !play;
        timer->start(5);
    }
}

void QImageLoader::loopImages(bool pressed) {
    if (images.size() > 0) {
        cerr << "Looping images" << endl;
        loop = !loop;
        play = !play;
        timer->start(5);
    }
}